package cn.lili.modules.payment.entity.dos;

import cn.hutool.json.JSONUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.payment.entity.dto.PayParam;
import cn.lili.mybatis.model.BaseEntity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serial;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 支付日志
 *
 * @author Chopper
 * @since 2021/1/28 09:21
 */
@Data
@TableName("li_payment_log")
@Builder
@ApiModel(value = "支付日志")
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class PaymentLog extends BaseEntity implements Cloneable {


    @Serial
    private static final long serialVersionUID = 647253814608116958L;


    @ApiModelProperty(value = "付款人ID")
    private String payerId;

    @ApiModelProperty(value = "订单编号")
    private String orderSn;

    @ApiModelProperty(value = "订单描述")
    private String description;

    /**
     * @see cn.lili.modules.payment.entity.enums.PaymentSceneEnums
     */
    @ApiModelProperty(value = "支付场景")
    private String paymentScene;

    /**
     * @see cn.lili.modules.payment.entity.enums.PaymentClientEnum
     */
    @ApiModelProperty(value = "订单来源")
    private String paymentClient;

    /**
     * @see cn.lili.modules.payment.entity.enums.PaymentMethodEnum
     */
    @ApiModelProperty(value = "支付方式")
    private String paymentMethod;

    @ApiModelProperty(value = "金额")
    private Double price;

    @ApiModelProperty(value = "已校验回查")
    private Boolean isCheck = false;

    @ApiModelProperty(value = "已付款")
    private Boolean isPay = false;

    @ApiModelProperty(value = "是否合单支付")
    private Boolean isCombine = false;

    @ApiModelProperty(value = "是否重复支付")
    private Boolean isRepeat = false;

    @ApiModelProperty(value = "重复支付后是否退款")
    private Boolean isRefund = false;

    @ApiModelProperty(value = "是否已结算")
    private Boolean isSettled = false;


    @ApiModelProperty(value = "合单支付单号")
    private String combineOutTradeNo;

    @ApiModelProperty(value = "第三方支付发起交易号")
    private String outTradeNo;

    @ApiModelProperty(value = "第三方平台付款流水号")
    private String transactionId;


    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "支付过期时间", hidden = true)
    private Date timeoutExpress;

    @CreatedDate
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间", hidden = true)
    private Date createTime;


    //收款人信息，如果是-1，则标识平台收款
    @ApiModelProperty(value = "收款人ID")
    private String payeeId;

    @ApiModelProperty(value = "付款人OPENID")
    private String openid;

    @ApiModelProperty(value = "商户号ID")
    private String mchId;


    @ApiModelProperty(value = "支付请求参数")
    private String paymentRequestSource;


    @ApiModelProperty(value = "支付响应")
    private String paymentResponseSource;

    @ApiModelProperty(value = "分账详细")
    private String receivers;

    public void setReceiverArray(List<Receiver> receivers) {
        this.receivers = JSONUtil.toJsonStr(receivers.toString());
    }

    public List<Receiver> getReceiverArray() {
        if (StringUtils.isEmpty(this.receivers)) {
            return new ArrayList<>();
        }
        return JSONUtil.toList(JSONUtil.parseArray(this.receivers), Receiver.class);
    }

    public PaymentLog(PayParam payParam) {
        this.orderSn = payParam.getCombineSn();
        this.paymentClient = payParam.getPaymentClientEnum().name();
        this.paymentMethod = payParam.getPaymentMethodEnum().getPlugin();
    }

    @Override
    public PaymentLog clone() throws CloneNotSupportedException {
        return (PaymentLog) super.clone();
    }
}

@Data
class Receiver {


    //分账接收方商户号
    private String mchId;

    //分账金额
    private Double amount;
}